package com.eascs.ant.data.controller;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.ant.data.entity.hr.HrFavorite;
import com.eascs.ant.data.entity.hr.HrMenu;
import com.eascs.ant.data.service.HrService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.sso.UserHolder;

@RestController
public class HrRestController extends BaseRestController {

	private static Logger logger = LoggerFactory.getLogger(HrRestController.class);
	@Inject
	private HrService hrService;
	
	@RequestMapping("/hr/delfr")
	public BaseRestResult<String> delFavorite(String id) {
		String account = UserHolder.getUser().getAccount();
		logger.info("操作员:{},删掉收藏简历id:{}", account , id);
		hrService.delFavorite(account, id);
		return BaseRestResult.refresh();
	}
	
	@RequestMapping("/hr/addFr")
	public BaseRestResult<String> addFavorite(String url) {
		//提取简历id
		String account = UserHolder.getUser().getAccount();
		String id = null;
		url = url.replaceAll("%26", "&");
		Matcher matcher = Pattern.compile("hidUserID=(.*?)&").matcher(url);
		if (matcher.find()) {
			id = matcher.group(1);
		} else {
			logger.error("不合法的简历链接:{}", url);
			return BaseRestResult.refresh();
		}
		logger.info("操作员:{},添加收藏简历id：{}", account , id);
		HrFavorite fr = new HrFavorite();
		fr.setAccount(account);
		List<HrMenu> list = new LinkedList<HrMenu>();
		HrMenu menu = new HrMenu();
		menu.setId(id);
		menu.setTime(new Date(System.currentTimeMillis()));
		menu.setUrl(url);
		list.add(menu);
		fr.setMenuList(list);
	    hrService.insertOrUpdate(fr);
		return BaseRestResult.refresh();
	}
}
